package cn.iocoder.yudao.module.alipay.service.mlong;

import cn.iocoder.yudao.module.alipay.util.HttpUtils;
import cn.iocoder.yudao.module.alipay.util.Md5Util;
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.io.UnsupportedEncodingException;
import java.util.Map;

@Service
public class MLongServiceImpl implements MLongService{


    @Override
    public Object getProducts() {
        String url = "https://haokaopenapi.lot-ml.com/api/order/GetProducts";
        Long timestamp = System.currentTimeMillis()/1000;
        String paramsStr = "ProductID="+""+"&Timestamp="+timestamp+
                "&user_id="+1452132353+"653cbd3db743b9d9925abe9f542a8bee";
        String sign = getSign(paramsStr);


        MultiValueMap<String,Object> map = new LinkedMultiValueMap<>();
        map.add("user_id",1452132353);
        map.add("Timestamp",timestamp);
        map.add("ProductID","");
        //交易简介
        map.add("user_sign",sign);


        return sendPostParams(url,map);
    }

    public  Map<String,Object> sendPostParams(String url, MultiValueMap<String,Object> map){
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        HttpEntity<MultiValueMap<String,Object>> requestEntity = new HttpEntity(map, headers);
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, requestEntity, String.class);
        String body = responseEntity.getBody();
        Map result = JSONObject.parseObject(body,Map.class);
        return result;
    }


    public String getSign(String signStr) {
        try{
            String md5 = Md5Util.encryptMd5(signStr);
            return md5;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return  null ;
        }
    }

}
